<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  created() {

  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    }

    // 监听侧边栏的变化
    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHanlder)
    }

    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

    this.chart.dispose()
    this.chart = null
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  computed: {

  },

  methods: {
    setOptions({ yArr, y, x, mar_low, mar_top, mar_up, mar_mean } = {}) {
      this.chart.setOption({
        color: ['#7CCD7C', '#d14a61', '#675bba'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        grid: {
          right: '20%'
        },
        toolbox: {
          feature: {
            dataView: {
              show: true,
              readOnly: false
            },
            restore: {
              show: true
            },
            saveAsImage: {
              show: true
            }
          }
        },
        legend: {
          data: ['原数据频率', '正态分布']
        },
        xAxis: [{
          type: 'category',
          axisTick: {
            alignWithLabel: true
          },
          data: x
        }],
        yAxis: [{
          type: 'value',
          name: '频率',
          position: 'right',
          axisLine: {
            lineStyle: {
              color: '#d14a61'
            }
          },
          axisLabel: {
            formatter: '{value}'
          }
        },
        {
          type: 'value',
          position: 'left',
          axisLine: {
            lineStyle: {
              color: '#7CCD7C'
            }
          },
          axisLabel: {
            formatter: '{value}'
          }
        }],
        series: [{
          name: '原数据频率',
          type: 'bar',
          yAxisIndex: 1,
          data: y
        },
        {
          name: '正态分布',
          type: 'line',
          smooth: true,
          yAxisIndex: 0,
          data: yArr
        },
        {
          name: '平行于y轴的趋势线',
          type: 'line',
          markLine: {
            itemStyle: { // 盒须图样式。
              color: '#000', // boxplot图形的颜色。 默认从全局调色盘 option.color 获取颜色
              normal: {
                lineStyle: {
                  width: 2,
                  type: 'solid' // 'dotted'虚线 'solid'实线
                },
                label: {
                  show: true,
                  position: 'end',
                  color: 'black',
                  formatter: function(params) {
                    const str = '-3μ'
                    return str
                  }
                }
              }
            },
            name: 'aa',
            data: [
              [{
                coord: [mar_low, 0]
              },
              {
                coord: [mar_low, mar_top]
              }]
            ]
          }
        },
        {
          name: '平行于y轴的趋势线',
          type: 'line',
          markLine: {
            itemStyle: { // 盒须图样式。
              color: '#000', // boxplot图形的颜色。 默认从全局调色盘 option.color 获取颜色
              normal: {
                lineStyle: {
                  width: 2,
                  type: 'solid' // 'dotted'虚线 'solid'实线
                },
                label: {
                  show: true,
                  position: 'end',
                  color: 'black',
                  formatter: function(params) {
                    const str = 'MEAN'
                    return str
                  }
                }
              }
            },
            name: 'bb',
            data: [
              [{
                coord: [mar_mean, 0]
              },
              {
                coord: [mar_mean, mar_top]
              }]
            ]
          }
        },
        {
          name: '平行于y轴的趋势线',
          type: 'line',
          markLine: {
            itemStyle: {
              color: '#000',
              normal: {
                lineStyle: {
                  width: 2,
                  type: 'solid'
                },
                label: {
                  show: true,
                  position: 'end',
                  color: 'black',
                  formatter: function(params) {
                    const str = '3μ'
                    return str
                  }
                }
              }
            },
            name: 'cc',
            yAxisIndex: 0,
            data: [
              [{
                coord: [mar_up, 0]
              },
              {
                coord: [mar_up, mar_top]
              }]
            ]
          }
        }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    }
  }
}
</script>
